<template>
	<div @touchmove.stop.prevent="moveHandle">
		<u-popup :show="show" mode="center" zIndex='99' duration="50">
			<view class="box-tk" v-if="list.children">
				<view class="view1">
					<view class="view1-title">麻醉药执行(残余量处理)</view>
				</view>
				<view class="xian"></view>
				<scroll-view scroll-y="true" v-if="list.children"
					:class="['box-xis',list.children.length > 3 ? 'box-aigicon':'']">
					<view class="xis-title" v-for="(el,index) in list.children" :key="index">
						<view class="" style="display:flex;justify-content: space-between">
							<text>{{el.ordertype}}</text>
							<text>{{el.admission}}</text>
							<text>{{el.plandate}}</text>
						</view>
						<view class="">
							<text>{{el.remark +"【" + el.dosage +" 】"+"【" + el.dosage +" 】"}}</text>
						</view>
					</view>
					<view class="box-img"></view>
					<view class="xis-hend" :class="[list.children.length>1 ? 'xis-avicon' :'']">
						<view class="" v-if="putongkoufu">
							<u-radio-group v-model="value" placement="row" activeColor="#7AD9DF !important"
								inactiveColor="#B0E9EC  !important">
								<u-radio :customStyle="{padding: '10px 0'}" v-for="(item, index) in radiolist"
									:key="index" :label="item.name" :name="item.name" @change="radioChange(item.id)"
									:class="value==item.name?'clao':'clat'">
								</u-radio>
							</u-radio-group>
							<view class="xis-yuanyin" v-if="yuanyinValue">
								<Select class="SelectList" :disabled="false" v-model="examinationTypeIndex"
									:options="teshuarray" @change="Selectchange"></Select>
								<u-textarea class="box-text" placeholder="请输入原因" maxlength="-1" v-model="yuanyin2"
									confirmType="done" :disabled="textBled"></u-textarea>
							</view>
						</view>
						<view class="hend-title">
							<text style="width: 120px;">残余量：</text>
							<u-input v-model="canyuliang" :disabled="true"></u-input>
						</view>
						<view class="hend-title">
							<text style="width: 120px;">产品批号：</text>
							<u-input v-model="chanpinpihao" maxlength="50"></u-input>
						</view>
						<view class="hend-title">
							<text style="width: 120px;">空安瓿数：</text>
							<u-input v-model="konganpeishu" type="number"></u-input>
						</view>
						<view class="hend-title">
							<text style="width: 120px;">备注：</text>
							<Select class="SelectList" :disabled="false" v-model="beizhuIndex" :options="beizhuarray"
								@change="SelectBeizhu"></Select>
							<!-- <u-input v-model="value2" @change="change"></u-input> -->
						</view>
						<view class="" style="display:flex;justify-content: flex-end;padding: 10px;">
							<view class="" style="width:calc(100% - 120px)">
								<u-input v-model="qita"></u-input>
							</view>
						</view>
						<view class="hend-title">
							<text style="width: 120px;">核对护士：</text>
							<u-input v-model="empid" placeholder="请输入工号" @change="change"></u-input>
							<view class="" style="width:90px;">
								<u-input v-model="empname" :disabled="true"></u-input>
							</view>
						</view>
						<view class="hend-title">
							<text style="width: 120px;">核对护士密码：</text>
							<u-input v-model="nscheckerpassword" placeholder="请输入密码" type="password"></u-input>
						</view>
					</view>
				</scroll-view>
				<view class="tk-heard" v-if="list.children">
					<u-button type="primary" style="margin-right: 10px;" text="确认" class="btn" color="#82ECC3"
						@tap="StopImplement"></u-button>
					<u-button type="primary" text="取消" class="btn" color="#FF6E91" @tap="cancel"></u-button>
				</view>
			</view>
		</u-popup>
	</div>
</template>

<script>
	import {
		EmpInfo,
		EMR_Nursing_SaveOrderMaZui_NS1
	} from "@/api/user.js"
	import {
		mapMutations,
		mapState
	} from "vuex";
	import Select from '@/pages/ListOfPatients/selevt.vue';
	import BigNumber from "@/components/js/bignumber.js";
	export default {
		props: {
			show: {
				type: Boolean,
				default: false
			},
			reportObj: {
				type: Object
			},
			reportShow: {
				type: Boolean
			},
			radiolist: {
				type: Array
			},
			teshuarray: {
				type: Array
			},
			
		},
		components: {
			Select
		},
		data() {
			return {
				// show2: true
				canyuliang: '',
				chanpinpihao: '',
				konganpeishu: '',
				qita: '',
				empid: '',
				empname: '',
				nscheckerpassword: '',
				time: '',
				list: [],
				resuList: [],
				yuanyinValue: '',
				examinationTypeIndex: 0,
				beizhuIndex: 0,
				textBled: true,
				putong_ady: '',
				yuanyinValue: false,
				radioValue: '',
				yuanyin2: '',
				placeholder: '',
				putongkoufu: false,
				beizhuarray: [{
						label: ' ',
						value: 0
					},
					{
						label: '双人核对后丢弃',
						value: 1
					},
					{
						label: '溶解双人核对后丢弃',
						value: 2
					},
					{
						label: '病人拒绝使用',
						value: 3
					},
					{
						label: '其他',
						value: 4
					}
				],
				canyuliangValues: '',
				dosagedaynum: '',
				pihao: '',
				konganpei: '',
				canyuliangVALUE: '',
				canyuliangnumnum: '',
				remark: '',
			}
		},
		computed: {
			...mapState('user', ["userInfo", "deleteObj", "scanningArray"]),
		},
		mounted() {
			this.value = this.radiolist[0].name
			this.list = this.reportObj
			if (this.list?.children) {
				if (this.list?.children.length > 0) {
					this.list.children.map(item => {
						if (item.admission.trim() == 'PO') {
							this.putongkoufu = true
						}
						this.canyuliangValues = (parseFloat(item.many).valueOf() / parseFloat(item.oppack)
							.valueOf())
							.valueOf().toString().trim() + "&" + item.drmodel
							.toString()
							.trim() + "&" + item.dosage.toString().trim()
						this.dosagedaynum = item.dosageday
					})
					this.JiSuanCan()
				}
			}

		},
		methods: {
			async change(e) {
				console.log(e);
				try {
					if (e.split(" ").join("").length != 0) {
						const user = uni.getStorageSync('userInfo')
						if (user.sUserid == e) {
							uni.showToast({
								title: "工号与登入工号相同！", //显示的文字
								duration: 1000, //显示多少时间，默认1500毫秒
								icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
								mask: true
							})
							this.empname = '工号与登入工号相同！'
						} else {
							const data = {
								HosKind: user.hoscode,
								empid: e
							}
							const res = await this.$http.EmpInfo(data)
							if (res[0]?.result == '1') {
								this.resuList = res
								this.empname = res[0].name
							} else {
								this.empname = res[0].result
							}
						}
					} else {
						this.empname = ''
						this.nscheckerpassword = ''
						return
					}
				} catch (e) {
					uni.showToast({
						title: "执行异常！", //显示的文字
						duration: 2000, //显示多少时间，默认1500毫秒
						icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					})
				}

			},
			cancel() {
				
				
				uni.showModal({
					title: '提示',
					content: '是否取消用药?',
					success:  (res) =>{
						if (res.confirm) {
						this.$emit('mayaoTion', false, this.reportObj.children, 'quxiao');
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				// this.show2 = false
			},
			StopImplement() {
				const user = uni.getStorageSync('userInfo')
				if (this.radioVaule == 'mid' && this.putong_ady == 6 && this.yuanyin2.trim() == '') {
					uni.showToast({
						title: "您选择了未服药其他，请输入原因！", //显示的文字
						duration: 2000, //显示多少时间，默认1500毫秒
						icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					})
					return
				}
				if (this.empname == '') {
					uni.showToast({
						title: "请输入核对护士工号！", //显示的文字
						duration: 1500, //显示多少时间，默认1500毫秒
						icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					})
					return
				} else if (user.sUserid == this.empid) {
					uni.showToast({
						title: "核对护士与登陆护士不能是同一个！", //显示的文字
						duration: 1500, //显示多少时间，默认1500毫秒
						icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					})
					return
				} else if (this.nscheckerpassword?.trim() == '') {
					uni.showToast({
						title: "请输入核对护士密码！", //显示的文字
						duration: 1500, //显示多少时间，默认1500毫秒
						icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					})
					return
				} else {
					if (this.resuList[0].password != this.nscheckerpassword) {
						uni.showToast({
							title: "核对护士密码错误！", //显示的文字
							duration: 1500, //显示多少时间，默认1500毫秒
							icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
							mask: true
						})
						return
					}
					if (this.list.children[0].admission.indexOf("IV") || this.list.children[0].admission.indexOf("IM") ||
						this.list.children[0].admission.indexOf("IVP") || this.list.children[0].admission.indexOf(
							"静脉注射") || this.list.children[0].admission.indexOf("肌注") || this.list.children[0].admission
						.indexOf("静脉注射(泵)")) {
						if (this.chanpinpihao.toString().trim() == "") {
							uni.showToast({
								title: "批号不能为空！", //显示的文字
								duration: 1500, //显示多少时间，默认1500毫秒
								icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
								mask: true
							})
							return
						}
					}
					this.DataZhiXing()
				}
			},
			DataZhiXing() {
				this.canyuliangVALUE = this.canyuliang.toString().trim();
				let cy = 0.0;
				let remark = ''
				try {
					cy = this.canyuliangnumnum.toString().trim();
				} catch (e) {}
				if (cy <= 0) {
					remark = "";
				} else {
					remark = this.beizhuarray[this.beizhuIndex].label;
				}
				let exeflag3 = "";
				if (this.radioValue == 'mid') {
					if (this.putong_ady == 6) {
						exeflag3 = this.yuanyin2.toString().trim();
					} else {
						exeflag3 = this.yuanyin2.toString().trim();
					}
				}
				console.log(exeflag3, '3', this.yuanyin2);
				this.pihao = this.chanpinpihao.toString().trim();
				if ("无需输入批号！" == this.pihao) {
					this.pihao = "";
				}
				this.konganpei = this.konganpeishu.toString();
				this.zhixingData(this.list.children, this.empid.toString().trim(), this.canyuliangVALUE, this.pihao, this
					.konganpei, remark, exeflag3)
			},
			async zhixingData(obj, nschecker, str1, str2, str3, str4, exeflag3) {
				try {
					let res;
					let user = uni.getStorageSync('userInfo')
					for (let i = 0; i < obj.length; i++) {
						const data = {
							HosKind: user.hoscode,
							sExemode: '',
							mrn: obj[i].mrn,
							stay: 'I',
							series: obj[i].series,
							orderdordnarray: obj[i].orderdordn,
							bedid: obj[i].bedid,
							pname: obj[i].pname,
							nschecker: nschecker,
							operdept: user.HosKind,
							operjp: user.sUserid,
							str1: str1,
							str2: str2,
							str3: str3,
							str4: str4,
							str5: obj[i].admission,
							iMark: obj[i].iMark,
							iautocharge: obj[i].iautocharge,
							exeflag3: this.yuanyin2
						}
						res = await this.$http.EMR_Nursing_SaveOrderMaZui_NS1(data)
					}
					if (res[0].result == '1') {
						uni.showToast({
							title: "医嘱执行成功！", //显示的文字
							duration: 1000, //显示多少时间，默认1500毫秒
							icon: "success", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						})
						this.$emit('mayaoTion', false, this.reportObj.children, 'queren');
						// return
					} else {
						uni.showToast({
							title: res[0].result, //显示的文字
							duration: 2000, //显示多少时间，默认1500毫秒
							icon: "none", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
							mask: true
						})
						this.$emit('mayaoTion', false, this.list.children, 'shibai',res[0].result);
						return
					}
				} catch (e) {
					uni.showToast({
						title: '执行异常！', //显示的文字
						duration: 2000, //显示多少时间，默认1500毫秒
						icon: "none", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					})
					//TODO handle the exception
				}
			},
			radioChange(data) {
				this.radioValue = data
				if (data == 'mid') {
					this.yuanyinValue = true
					this.examinationTypeIndex = 0
					this.yuanyin2 = this.teshuarray[this.examinationTypeIndex].label
				} else {
					this.yuanyinValue = false
					this.yuanyin2 = ''
				}
			},
			Selectchange(e) {
				this.putong_ady = e
				if (e == 6) {
					this.yuanyin2 = ''
					this.textBled = false
				} else {
					this.yuanyin2 = this.teshuarray[e].label
					this.textBled = true
				}
			},
			SelectBeizhu(e) {
				this.beizhuIndex = e
			},
			TimePicker(data, value) {
				this.placeholder = data
			},
			JiSuanCan() {
				// 残余量计算 //dosageday * drmodel -dosage
				var canyuliangNum = 0.00;
				let dosage_unit = "";
				let dosage_num = "";
				try {
					if (this.canyuliangValues != "") {
						let args = this.canyuliangValues.split("&");
						let dosageday = args[0];
						let drmodel = args[1];
						let dosage = args[2];
						dosage_num = this.RemoveNotNumber(dosage); // 每次剂量
						// dosage_unit = dosage.replace(/[0-9]*/g, '').replace(/./, ''); // 单位
						dosage_unit = dosage.match(/(\d+)([a-z]+)/i)[2]
						drmodel = drmodel.replace("：", ":").toString();
						if (drmodel.indexOf(":")) {
							let drmodel_str = drmodel.split(":");
							for (let i = 0; i < drmodel_str.length; i++) {
								if (drmodel_str[i].toString().indexOf(dosage_unit)) {
									drmodel = drmodel_str[i].toString().replace(
										dosage_unit, "");
								}
							}
						} else {
							drmodel = this.RemoveNotNumber(drmodel);
						}
						let d1 = parseFloat(dosageday.valueOf());
						let d2 = parseFloat(drmodel.valueOf());
						let d3 = parseFloat(dosage_num.valueOf());
						canyuliangNum = new BigNumber(d1).multipliedBy(new BigNumber(d2).minus(d3).toNumber()).toNumber()
						if (canyuliangNum > 0) {
							this.canyuliang = (canyuliangNum.toString().trim().toString()) +
								dosage_unit;
							this.canyuliangnumnum = (canyuliangNum.toString().trim()).toString();
						} else {
							this.canyuliang = ("0" + dosage_unit);
							this.canyuliangnumnum = "0";
						}
					}
				} catch (e) {}
				if (this.list.children[0].admission == 'PO') {
					this.putongkoufu = true
					this.chanpinpihao = "无需输入批号！";
					this.pihao = "";

					if (canyuliangNum > 0) {
						this.beizhuIndex = 2
						// beizhu.setSelection(2);
					} else {
						this.beizhuIndex = 0
						// beizhu.setSelection(0);
					}
				} else {
					if (canyuliangNum > 0) {
						this.beizhuIndex = 1
						// beizhu.setSelection(1);
					} else {
						this.beizhuIndex = 0
						// beizhu.setSelection(0);
					}
				}
				if ("TAB" == this.list.children[0].oppackunit.trim()) {
					this.konganpeishu = "0";
					this.konganpei = "0";
				} else if ("AMP" == this.list.children[0].oppackunit.trim()) {
					this.konganpeishu = this.dosagedaynum;
					this.konganpei = this.dosagedaynum;
				}
				this.konganpeishu = this.konganpeishu.toFixed(1)
				// nscheckerid.setFocusable(true);
				// nscheckerid.setFocusableInTouchMode(true);
				// nscheckerid.requestFocus();
				// nscheckerid.requestFocusFromTouch();
			},
			RemoveNotNumber(key) {
				return key.replace("[^\\d|.]*", "");
			},
			moveHandle() {}
		}
	}
</script>


<style scoped lang="scss">
	::v-deep .u-popup {
		.u-fade-enter-active {
			z-index: 99 !important;
		}
	}

	.xis-yuanyin {
		display: flex;
		align-items: center;
		height: 30px;
		margin: 0 6px;
		padding-bottom: 9px;


	}

	.SelectList {
		width: calc(100% - 120px) !important;
	}

	.box-tk {
		width: 670rpx;
		// height: 212rpx;
		// background: #FFFFFF;
		border-radius: 8rpx;
		position: relative;
		padding-bottom: 15rpx;

		.view1 {
			font-size: 36rpx;
			font-family: SourceHanSansCN-Medium, SourceHanSansCN;
			font-weight: 500;
			color: #487bf4;
			line-height: 40px;
			text-align: center;
			display: flex;
			justify-content: space-evenly;
			align-items: center;

			.view1-title {
				color: #333333;

			}
		}

		.xian {
			position: absolute;
			width: 100%;
			height: 2rpx;
			background: #7AD9DF;
			left: 0;
			top: 80rpx;
			// opacity: 0.19;
			// opacity: 0.19;
			// margin: 14rpx 0;
		}

		.box-xis ::v-deep {
			width: 97%;
			// height: 92%;
			background: #B0E9EC;
			margin: 20rpx auto 0;
			padding-bottom: 12rpx;
			position: relative;
			border-radius: 12rpx;
			z-index: 99999;

			.uni-scroll-view {
				overflow: inherit !important;
			}

			.uni-scroll-view-content {
				width: 100% !important;
			}

			.box-img {
				position: absolute;
				right: 0;
				top: 0;
				width: 58px;
				height: 28px;
				background: url('../../static/img/coagulation.svg') no-repeat;
				background-size: 100% auto;
				box-shadow: 5px -3px 8px 0px #D4D9D9;
			}

			.xis-title {
				font-size: 16px;
				font-family: SourceHanSansCN-Normal, SourceHanSansCN;
				font-weight: 400;
				color: #333333;
				padding: 4rpx 18rpx;
				line-height: 28px;
				// height: 28px;
				width: 80%;
			}

			.xis-hend {
				// width: 303px;
				width: 96%;
				// height: calc(100%);
				// height: 81%;
				margin: 0 auto;
				// height: 228px;
				background: #F6FEFF;
				border-radius: 6px;
				opacity: 0.95;

				text {
					font-size: 32rpx;
					font-family: SourceHanSansCN-Medium, SourceHanSansCN;
					font-weight: 500;
					// color: #333333;
					line-height: 48rpx;
					// width: 66px;
					text-align: justify;
					text-align-last: justify;
				}

				.hend-title {
					font-size: 32rpx;
					font-family: SourceHanSansCN-Medium, SourceHanSansCN;
					// font-weight: 500;
					color: #333333;
					line-height: 48rpx;
					padding: 20rpx;
					display: flex;
					align-items: center;

					.text {
						width: 65px;
					}

					.hend-time {
						width: calc(100% - 65px);
					}
				}
			}
		}

		.tk-heard {
			display: flex;
			// height: calc(100% - 636rpx);
			align-items: center;
			margin: 16rpx auto 0;
			width: 97%;
		}

		.tk-heard ::v-deep {
			.u-button__text {
				font-size: 36rpx !important;
				font-family: SourceHanSansCN-Medium, SourceHanSansCN;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 54rpx;
				letter-spacing: 28rpx;
			}
		}
	}

	::v-deep .u-radio__icon-wrap--circle {
		width: 26px !important;
		height: 26px !important;
		background-color: rgb(122, 217, 223) !important;
	}

	::v-deep .u-radio-group--row {
		justify-content: space-around;
	}

	.clao ::v-deep {
		.u-icon__icon {
			border-radius: 100%;
			background: #FEFF87;
			color: #FEFF87 !important;
			width: 13px !important;
			height: 13px !important;
			// font-size: 16px !important;
		}
	}

	.clat ::v-deep {
		.u-icon__icon {
			border-radius: 100%;
			background: #FFFFFF;
			// font-size: 16px !important;
			width: 13px !important;
			height: 13px !important;
		}
	}

	.box-text {
		padding: 7px !important;
	}

	.u-textarea::v-deep {
		.u-textarea__field {
			height: 100% !important;
		}

		.uni-textarea-wrapper {
			overflow: hidden !important;
		}
	}

	.box-aigicon {
		height: 320px;
	}
</style>
